package cn.sfa.common.jms;

import javax.jms.Destination;
import javax.jms.Queue;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;

import cn.sfa.service.news.PreNewsService;
/**
 * 
* 类名称：ResourceMessageProducer   
* 类描述： 消息生产者
* 创建人：lenovo   
* 创建时间：2012-2-9 下午6:06:50    
* 修改备注：   
* @version v1.0
 */
public class ResourceMessageProducer {
	private Logger log = LoggerFactory.getLogger(ResourceMessageProducer.class);
	
	private JmsTemplate template;
	
	private Queue destination;

	public void setTemplate(JmsTemplate template) {
		this.template = template;
	}

	public void setDestination(Queue destination) {
		this.destination = destination;
	}

	public void  send(Object obj) {
		log.info("=======================================");
		log.info("do send ......");
		long l1=System.currentTimeMillis();
		template.convertAndSend(this.destination, obj);
		log.info("send time:"+(System.currentTimeMillis()-l1)/1000+"s");
		log.info("=======================================发送结束");
	}
	
	public void sendNews(Object obj)
	{
		template.convertAndSend(this.destination,obj);
	}
	@Test
	public void test() {
		ApplicationContext app=new ClassPathXmlApplicationContext("configuration/applicationContext-jms.xml");
		template=(JmsTemplate)app.getBean(JmsTemplate.class);
		this.destination=(Queue)app.getBean(Destination.class);
		for(int i=0;i<5;i++)
		{
			Book book=new Book();
			book.setAuthor("autoher"+i);
			book.setName("作者"+i);
			book.setId(i);
			//send(book);
			
		}
		ResourceMessageConsumer consumer=new ResourceMessageConsumer();
		//consumer.addResource(new Book());
	}
}
